import os
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# 对应书中的第4章
# ************************** 4.3 webDriver常用属性及方法 ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# search = driver.find_element_by_id("kw") # 定位到搜索框
# search.clear() # 清空输入框中的内容
# search.send_keys("selenium") # 输入搜索内容
# search.submit() # 提交表单->相当于输入完文本后直接按回车按钮

# time.sleep(3)
# driver.find_element_by_id("su").click() #点击“百度一下”按钮
# time.sleep(3)

# print(search.size) # 获取输入框的尺寸
# print(driver.find_element_by_xpath("//div[@class='s-bottom-layer-content']/p[7]").text) #获取元素的文本
# print(search.is_displayed()) # 返回的元素是否可见
# # 获取元素控件的属性 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
# print(search.get_attribute("autocomplete"))

# ************************** 4.4 鼠标操作 ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# 定位到欲悬停的元素
# above = driver.find_element_by_link_text("更多")
#
# action_chains = ActionChains(driver)
# executor = action_chains.move_to_element(above) # 对定位到的元素进行悬停
# # executor = action_chains.context_click(above) #右击元素
# # executor = action_chains.double_click(above) # 双击元素
# executor = action_chains.drag_and_drop(above, above) # 将元素拖拽到指定元素上
# executor.perform() # 使鼠标动作生效


# ************************** 4.6 获取验证信息 ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# print("before url: % s" % driver.current_url) # 获取当前页面链接
# print("before title: % s" % driver.title) # 获取当前页面标题
# search = driver.find_element_by_id("kw")
# search.clear()
# search.send_keys("selenium")
# driver.find_element_by_id("su").click()
# time.sleep(2)
#
# print("after url: % s" % driver.current_url)
# print("after title: % s" % driver.title)
# print("result:", driver.find_element_by_xpath("//*[@id='tsn_inner']/div[2]/span").text)

# ************************** 4.7 设置元素等待 ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# 显示等待,即:显式地设置某个条件(一般是某个元素是否出现或消失),
# 每隔一定的时间去检测此条件是否满足,若在达到最大时间后,待检测的条件仍未为True,则抛出异常
# expected_condition的常见方法见书 P61
#
# element = WebDriverWait(driver, 5, 0.5).until(
#     EC.visibility_of_element_located(By.ID, "kw")
# )
# element.send_keys("selenium")
#
# # 隐式等待,设置的最大等待时间不影响脚本的运行速度,会等待页面上所有的元素,
# # 当脚本执行到定位某个元素时,若元素存在,则继续执行;否则一直轮询,直到最大时间仍未找到则抛出异常
# driver.implicitly_wait(10) #隐式等待
# try:
#     driver.find_element_by_id("kw22").send_keys("selenium")
# except NoSuchElementException as e:
#     print(e)
# finally:
#     driver.quit()


# ************************** 4.8 定位一组元素, find_element s,返回值是列表 ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# driver.find_element_by_id("kw").send_keys("selenium")
# driver.find_element_by_id("su").click()
# time.sleep(2)
# texts = driver.find_elements_by_xpath("//div[@tpl='se_com_default']/div/div/h3/a") #返回值的数据类型是列表
# print("numbers of result: %d" % (len(texts)))
# for text in texts:
#     print(text.text)

# ************************** 4.9 多表单切换 ****************************
# driver = webdriver.Chrome()
# url_163_mail = "https://mail.163.com/"
# driver.maximize_window()
# driver.get(url_163_mail)
#
# login_frame = driver.find_element_by_css_selector("iframe[id^='x-URS-iframe']")
# driver.switch_to.frame(login_frame) # 跳转到登录表单
# driver.find_element_by_xpath("//input[@name='email']").send_keys("18390862318")
# driver.find_element_by_xpath("//input[@name='password']").send_keys("litan19970209")
# driver.find_element_by_id("dologin").click()
# driver.switch_to.default_content()
# time.sleep(3)

# ************************** 4.10 多窗口切换 ****************************
# driver = webdriver.Chrome()
# driver.implicitly_wait(10)
# driver.get("http://www.baidu.com")
# # 获取当前窗口(百度搜索)的句柄
# search_windows = driver.current_window_handle
#
# driver.find_element_by_link_text("登录").click()
# driver.find_element_by_link_text("立即注册").click()
# all_handles = driver.window_handles # 获取当前打开的所有窗口的句柄
# for handle in all_handles:
#     if handle != search_windows:
#         driver.switch_to.window(handle) # 跳转到指定的窗口
#         print("Register Page Title:", driver.title)
#         driver.find_element_by_name("userName").send_keys("Litan")
#         driver.find_element_by_name("phone").send_keys("1839086****")
#         time.sleep(2)
#         driver.close()
#
# driver.switch_to.window(search_windows)
# print("Search Page Title:", driver.title)

# ************************** 4.11 警告框处理 ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# driver.find_element_by_id("s-usersetting-top").click() # 点击设置
# driver.find_element_by_link_text("搜索设置").click()
# time.sleep(3)
# try:
#     driver.find_element_by_class_name("prefpanelgo").click()
# # 获取弹窗
#     alert = driver.switch_to.alert
#     # 获取弹窗文本
#     alert_text = alert.text
#     print("alert_text:",alert_text)
#
#     # 在alert上点击确认
#     alert.accept()
#
# # 或取消alert
# # alert.dismiss()
# except NoSuchElementException as ne:
#     print("Exception:",ne)
# finally:
#     driver.quit()


# ************************** 4.14 下载文件 ****************************
# options = webdriver.ChromeOptions()
# prefs = {'profile.default_content_setting.popups': 0, # 禁止弹出下载窗口
#          'download.default_directory': os.getcwd() # 设置下载目录
#          }
#
# options.add_experimental_option('prefs', prefs)
#
# driver = webdriver.Chrome(options=options)
# driver.get("https://pypi.org/project/selenium/#files")
# driver.find_element_by_partial_link_text("selenium-4.4.3-py3-none-any.whl").click()
# time.sleep(10)

# ************************** 4.15 操作cookie ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
# # 获取所有的cookie信息，字典形式存储
# cookies = driver.get_cookies()
#
# # 添加cookie
# driver.add_cookie({"name": "key-aaaa", "value": "value-bbbb"})
# cookies = driver.get_cookies()
# for cookie in cookies:
#     print("%s -> %s" % (cookie['name'], cookie['value']))

# ************************** 4.16 调用JS ****************************
# driver = webdriver.Chrome()
# driver.get("http://www.baidu.com")
#
# driver.set_window_size(800, 600)
# driver.find_element_by_id("kw").send_keys("selenium")
# driver.find_element_by_id("su").click()
#
# js = "window.scrollTo(100, 450)"
# driver.execute_script(js)
# time.sleep(3)

# ************************** 4.17 处理HTML5视频播放 ****************************
# driver = webdriver.Chrome()
# driver.maximize_window()
# driver.get("https://videojs.com/")
# video = driver.find_element_by_id("vjs_video_3_html5_api")
# url = driver.execute_script("return arguments[0].currentSrc;", video)
# print("url=",url)
# # 播放视频
# print("start")
# driver.execute_script("arguments[0].play()", video)
#
# time.sleep(15)
# # 暂停视频
# print("stop")
# driver.execute_script("arguments[0].pause()", video)
#
#
# driver.quit()

# ************************** 4.19 窗口截图 ****************************
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.save_screenshot("baidu_demo.png")
driver.quit()